import java.util.Arrays;

public class MyStack {

    public int[] elem;
    public int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyStack(){
        this.elem = new int[DEFAULT_SIZE];
    }

    //压栈
    public void push(int val){
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length); //扩容
        }
        this.elem[usedSize] = val;
        usedSize++;
//        return val;
    }

    //判断栈满
    public boolean isFull(){
        return usedSize == elem.length;
    }

    //出栈
    public int pop(){
        if(empty()){
            throw new EmptyStackException("栈为空！");
        }
        /*int ret = elem[usedSize-1];
        usedSize--;
        return ret;*/
        return elem[--usedSize];
    }

    //判断栈空
    public boolean empty(){
        return usedSize == 0;
    }

    //返回栈顶
    public int peek(){
        if(empty()){
            throw new EmptyStackException("栈为空！");
        }
        return elem[usedSize-1]; //因为peek()函数只返回栈顶并不移动栈顶，所以这里不能使用elem[--usedSize]
    }
}
